﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace RefOffPDA
{
    /*
     * Class: C1Code
     * 
     * Use: Used to indicate the chosen disciplinary code
     *      for a C1 caution (unsporting behaviour)
     *      
     * Author: Mike Skaife, mcs3@aber.ac.uk
     */ 
    public partial class C1Code : Form
    {
        //Details required for connecting
        //to the database
        string conString;
        SqlCeConnection conn;

        //The current Match object
        Match theMatch;

        //Indicate if the home or away team is
        //receiving the caution
        public String homeOrAway;

        //id of the current Match object
        int matchId;

        //Buttons from other screens which need to
        //be updated as a result of the caution
        public Button sendOffButton;
        public Button goalButton;
        public Button cautionButton;
        public Button subOffButton;

        //id of the player receiving the caution
        public int player_id;

        //Used to hold the chosen C1 disciplinary code
        String code;

        public C1Code(Match match)
        {
            InitializeComponent();
            theMatch = match;
        }

        //This method is used to record the C1 caution
        //in the database
        public void cautionPlayer()
        {
            //Obtain the current GPS location of the
            //device for use later on Virtual Pitch
            String gps = getGPS();

            //Connect to the database
            connectToDatabase();

            //Set id of current Match for use in query
            matchId = theMatch.match_id;

            //Current value of match timer, to indicate
            //when incident occurred
            String timeOfIncident = theMatch.tenMinutes + theMatch.minutes + ":" + theMatch.tenSeconds + theMatch.seconds;
            
            //Create and execute SQL command to record caution in database,
            //with all required parameters
            string command = @"INSERT INTO caution(caution_code,c1_code,time,player_id,match_id,incident_GPS) VALUES(@c1,@code,@time,@id,@match,@incidentGPS);";
            using (SqlCeCommand c1_caution = new SqlCeCommand(command, conn))
            {
                //Caution code will be C1
                c1_caution.Parameters.AddWithValue("@c1", "C1");
                c1_caution.Parameters.AddWithValue("@code", code);
                c1_caution.Parameters.AddWithValue("@time", timeOfIncident);
                c1_caution.Parameters.AddWithValue("@id", player_id);
                c1_caution.Parameters.AddWithValue("@match", matchId);
                c1_caution.Parameters.AddWithValue("@incidentGPS", gps);
                c1_caution.ExecuteNonQuery();
            }

            //Disconnect from database
            closeDatabaseConnection();
        }

        //This method is called if the C1 caution is the second
        //caution for the player involved, meaning they also have
        //to be sent off
        public void cautionPlayerOff()
        {
            //Obtain current GPS location of PDA device
            //for later use on Virtual Pitch
            String gps = getGPS();

            //Connect to the database
            connectToDatabase();
            
            //id of the current Match for use in query
            matchId = theMatch.match_id;

            //Current value of match timer to indicate
            //when incident occurred
            String timeOfIncident = theMatch.tenMinutes + theMatch.minutes + ":" + theMatch.tenSeconds + theMatch.seconds;
            
            //Create and execute SQL command to record caution
            string command = @"INSERT INTO caution(caution_code,c1_code,time,player_id,match_id,incident_GPS) VALUES(@c1,@code,@time,@id,@match,@incidentGPS);";
            using (SqlCeCommand c1_caution = new SqlCeCommand(command, conn))
            {
                //Caution code will always be C1
                c1_caution.Parameters.AddWithValue("@c1", "C1");
                c1_caution.Parameters.AddWithValue("@code", code);
                c1_caution.Parameters.AddWithValue("@time", timeOfIncident);
                c1_caution.Parameters.AddWithValue("@id", player_id);
                c1_caution.Parameters.AddWithValue("@match", matchId);
                c1_caution.Parameters.AddWithValue("@incidentGPS", gps);
                c1_caution.ExecuteNonQuery();
            }

            //Disconnect from the database
            closeDatabaseConnection();

            //Connect to database to record the sending off
            connectToDatabase();

            //Create and execute SQL command to record the resulting
            //sending off in the database
            string off_command = @"INSERT INTO sendoff(sendoff_code,time,player_id,match_id,incident_GPS) VALUES(@code,@time,@id,@match,@incidentGPS);";
            using (SqlCeCommand c1_caution_player_off_off = new SqlCeCommand(off_command, conn))
            {
                //Sending off code will be S7, to indicate player has been
                //sent off for receving a second caution in the same match
                c1_caution_player_off_off.Parameters.AddWithValue("@code", "S7");
                c1_caution_player_off_off.Parameters.AddWithValue("@time", timeOfIncident);
                c1_caution_player_off_off.Parameters.AddWithValue("@id", player_id);
                c1_caution_player_off_off.Parameters.AddWithValue("@match", matchId);
                c1_caution_player_off_off.Parameters.AddWithValue("@incidentGPS", gps);
                c1_caution_player_off_off.ExecuteNonQuery();
            }

            //Disconnect from the database
            closeDatabaseConnection();
        }

        //This method is run if the chosen C1 code is 'AA'
        private void C1CodeAABtn_Click(object sender, EventArgs e)
        {
            //Indicate the chosen C1 code
            code = "AA";

            //If this is the players first caution...
            if (!cautionButton.BackColor.Equals(Color.Yellow))
            {
                //Colour all relevant buttons on other screens
                //to indicate caution being recorded
                sendOffButton.BackColor = Color.Yellow;
                goalButton.BackColor = Color.Yellow;
                cautionButton.BackColor = Color.Yellow;
                subOffButton.BackColor = Color.Yellow;

                //Record the actual caution
                cautionPlayer();
            }
            //Otherwise this is the players second caution...
            else
            {
                //Inform the referee that the player should also be sent off
                MessageBox.Show("This player is being sent off", "Second Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                
                //Colour relevant buttons from other screens, and
                //disable them as a result of player being sent off
                sendOffButton.BackColor = Color.Red;
                sendOffButton.Enabled = false;
                goalButton.BackColor = Color.Red;
                goalButton.Enabled = false;
                cautionButton.BackColor = Color.Red;
                cautionButton.Enabled = false;
                subOffButton.BackColor = Color.Red;
                subOffButton.Enabled = false;

                //Record the actual caution and resulting sending off
                cautionPlayerOff();
            }

            //Hide this screen to display the main Match screen
            this.Visible = false;
        }

        //This method is run if the chosen C1 code is 'DI'
        private void C1CodeDIBtn_Click(object sender, EventArgs e)
        {
            //Indicate the chosen C1 code
            code = "DI";

            //If this is the players first caution...
            if (!cautionButton.BackColor.Equals(Color.Yellow))
            {
                //Colour all relevant buttons on other screens
                //to indicate caution being recorded
                sendOffButton.BackColor = Color.Yellow;
                goalButton.BackColor = Color.Yellow;
                cautionButton.BackColor = Color.Yellow;
                subOffButton.BackColor = Color.Yellow;

                //Record the actual caution
                cautionPlayer();
            }
            //Otherwise this is the players second caution...
            else
            {
                //Inform the referee that the player should also be sent off
                MessageBox.Show("This player is being sent off", "Second Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                //Colour relevant buttons from other screens, and
                //disable them as a result of player being sent off
                sendOffButton.BackColor = Color.Red;
                sendOffButton.Enabled = false;
                goalButton.BackColor = Color.Red;
                goalButton.Enabled = false;
                cautionButton.BackColor = Color.Red;
                cautionButton.Enabled = false;
                subOffButton.BackColor = Color.Red;
                subOffButton.Enabled = false;

                //Record the actual caution and resulting sending off
                cautionPlayerOff();
            }

            //Hide this screen to display the main Match screen
            this.Visible = false;
        }

        //This method is run if the chosen C1 code is 'DP'
        private void C1CodeDPBtn_Click(object sender, EventArgs e)
        {
            //Indicate the chosen C1 code
            code = "DP";

            //If this is the players first caution...
            if (!cautionButton.BackColor.Equals(Color.Yellow))
            {
                //Colour all relevant buttons on other screens
                //to indicate caution being recorded
                sendOffButton.BackColor = Color.Yellow;
                goalButton.BackColor = Color.Yellow;
                cautionButton.BackColor = Color.Yellow;
                subOffButton.BackColor = Color.Yellow;

                //Record the actual caution
                cautionPlayer();
            }
            //Otherwise this is the players second caution...
            else
            {
                //Inform the referee that the player should also be sent off
                MessageBox.Show("This player is being sent off", "Second Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                //Colour relevant buttons from other screens, and
                //disable them as a result of player being sent off
                sendOffButton.BackColor = Color.Red;
                sendOffButton.Enabled = false;
                goalButton.BackColor = Color.Red;
                goalButton.Enabled = false;
                cautionButton.BackColor = Color.Red;
                cautionButton.Enabled = false;
                subOffButton.BackColor = Color.Red;
                subOffButton.Enabled = false;

                //Record the actual caution and resulting sending off
                cautionPlayerOff();
            }

            //Hide this screen to display the main Match screen
            this.Visible = false;
        }

        //This method is run if the chosen C1 code is 'FT'
        private void C1CodeFTBtn_Click(object sender, EventArgs e)
        {
            //Indicate the chosen C1 code
            code = "FT";

            //If this is the players first caution...
            if (!cautionButton.BackColor.Equals(Color.Yellow))
            {
                //Colour all relevant buttons on other screens
                //to indicate caution being recorded
                sendOffButton.BackColor = Color.Yellow;
                goalButton.BackColor = Color.Yellow;
                cautionButton.BackColor = Color.Yellow;
                subOffButton.BackColor = Color.Yellow;

                //Record the actual caution
                cautionPlayer();
            }
            //Otherwise this is the players second caution...
            else
            {
                //Inform the referee that the player should also be sent off
                MessageBox.Show("This player is being sent off", "Second Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                //Colour relevant buttons from other screens, and
                //disable them as a result of player being sent off
                sendOffButton.BackColor = Color.Red;
                sendOffButton.Enabled = false;
                goalButton.BackColor = Color.Red;
                goalButton.Enabled = false;
                cautionButton.BackColor = Color.Red;
                cautionButton.Enabled = false;
                subOffButton.BackColor = Color.Red;
                subOffButton.Enabled = false;

                //Record the actual caution and resulting sending off
                cautionPlayerOff();
            }

            //Hide this screen to display the main Match screen
            this.Visible = false;
        }

        //This method is run if the chosen C1 code is 'GC'
        private void C1CodeGCBtn_Click(object sender, EventArgs e)
        {
            //Indicate the chosen C1 code
            code = "GC";

            //If this is the players first caution...
            if (!cautionButton.BackColor.Equals(Color.Yellow))
            {
                //Colour all relevant buttons on other screens
                //to indicate caution being recorded
                sendOffButton.BackColor = Color.Yellow;
                goalButton.BackColor = Color.Yellow;
                cautionButton.BackColor = Color.Yellow;
                subOffButton.BackColor = Color.Yellow;

                //Record the actual caution
                cautionPlayer();
            }
            //Otherwise this is the players second caution...
            else
            {
                //Inform the referee that the player should also be sent off
                MessageBox.Show("This player is being sent off", "Second Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                //Colour relevant buttons from other screens, and
                //disable them as a result of player being sent off
                sendOffButton.BackColor = Color.Red;
                sendOffButton.Enabled = false;
                goalButton.BackColor = Color.Red;
                goalButton.Enabled = false;
                cautionButton.BackColor = Color.Red;
                cautionButton.Enabled = false;
                subOffButton.BackColor = Color.Red;
                subOffButton.Enabled = false;

                //Record the actual caution and resulting sending off
                cautionPlayerOff();
            }

            //Hide this screen to display the main Match screen
            this.Visible = false;
        }

        //This method is run if the chosen C1 code is 'HB'
        private void C1CodeHBBtn_Click(object sender, EventArgs e)
        {
            //Indicate the chosen C1 code
            code = "HB";

            //If this is the players first caution...
            if (!cautionButton.BackColor.Equals(Color.Yellow))
            {
                //Colour all relevant buttons on other screens
                //to indicate caution being recorded
                sendOffButton.BackColor = Color.Yellow;
                goalButton.BackColor = Color.Yellow;
                cautionButton.BackColor = Color.Yellow;
                subOffButton.BackColor = Color.Yellow;

                //Record the actual caution
                cautionPlayer();
            }
            //Otherwise this is the players second caution...
            else
            {
                //Inform the referee that the player should also be sent off
                MessageBox.Show("This player is being sent off", "Second Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                //Colour relevant buttons from other screens, and
                //disable them as a result of player being sent off
                sendOffButton.BackColor = Color.Red;
                sendOffButton.Enabled = false;
                goalButton.BackColor = Color.Red;
                goalButton.Enabled = false;
                cautionButton.BackColor = Color.Red;
                cautionButton.Enabled = false;
                subOffButton.BackColor = Color.Red;
                subOffButton.Enabled = false;

                //Record the actual caution and resulting sending off
                cautionPlayerOff();
            }

            //Hide this screen to display the main Match screen
            this.Visible = false;
        }

        //This method is run if the chosen C1 code is 'RP'
        private void C1CodeRPBtn_Click(object sender, EventArgs e)
        {
            //Indicate the chosen C1 code
            code = "RP";

            //If this is the players first caution...
            if (!cautionButton.BackColor.Equals(Color.Yellow))
            {
                //Colour all relevant buttons on other screens
                //to indicate caution being recorded
                sendOffButton.BackColor = Color.Yellow;
                goalButton.BackColor = Color.Yellow;
                cautionButton.BackColor = Color.Yellow;
                subOffButton.BackColor = Color.Yellow;

                //Record the actual caution
                cautionPlayer();
            }
            //Otherwise this is the players second caution...
            else
            {
                //Inform the referee that the player should also be sent off
                MessageBox.Show("This player is being sent off", "Second Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                //Colour relevant buttons from other screens, and
                //disable them as a result of player being sent off
                sendOffButton.BackColor = Color.Red;
                sendOffButton.Enabled = false;
                goalButton.BackColor = Color.Red;
                goalButton.Enabled = false;
                cautionButton.BackColor = Color.Red;
                cautionButton.Enabled = false;
                subOffButton.BackColor = Color.Red;
                subOffButton.Enabled = false;

                //Record the actual caution and resulting sending off
                cautionPlayerOff();
            }

            //Hide this screen to display the main Match screen
            this.Visible = false;
        }

        //This method is run if the chosen C1 code is 'SP'
        private void C1CodeSPBtn_Click(object sender, EventArgs e)
        {
            //Indicate the chosen C1 code
            code = "SP";

            //If this is the players first caution...
            if (!cautionButton.BackColor.Equals(Color.Yellow))
            {
                //Colour all relevant buttons on other screens
                //to indicate caution being recorded
                sendOffButton.BackColor = Color.Yellow;
                goalButton.BackColor = Color.Yellow;
                cautionButton.BackColor = Color.Yellow;
                subOffButton.BackColor = Color.Yellow;

                //Record the actual caution
                cautionPlayer();
            }
            //Otherwise this is the players second caution...
            else
            {
                //Inform the referee that the player should also be sent off
                MessageBox.Show("This player is being sent off", "Second Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                //Colour relevant buttons from other screens, and
                //disable them as a result of player being sent off
                sendOffButton.BackColor = Color.Red;
                sendOffButton.Enabled = false;
                goalButton.BackColor = Color.Red;
                goalButton.Enabled = false;
                cautionButton.BackColor = Color.Red;
                cautionButton.Enabled = false;
                subOffButton.BackColor = Color.Red;
                subOffButton.Enabled = false;

                //Record the actual caution and resulting sending off
                cautionPlayerOff();
            }

            //Hide this screen to display the main Match screen
            this.Visible = false;
        }

        //This method is run if the chosen C1 code is 'TR'
        private void C1CodeTRBtn_Click(object sender, EventArgs e)
        {
            //Indicate the chosen C1 code
            code = "TR";

            //If this is the players first caution...
            if (!cautionButton.BackColor.Equals(Color.Yellow))
            {
                //Colour all relevant buttons on other screens
                //to indicate caution being recorded
                sendOffButton.BackColor = Color.Yellow;
                goalButton.BackColor = Color.Yellow;
                cautionButton.BackColor = Color.Yellow;
                subOffButton.BackColor = Color.Yellow;

                //Record the actual caution
                cautionPlayer();
            }
            //Otherwise this is the players second caution...
            else
            {
                //Inform the referee that the player should also be sent off
                MessageBox.Show("This player is being sent off", "Second Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                //Colour relevant buttons from other screens, and
                //disable them as a result of player being sent off
                sendOffButton.BackColor = Color.Red;
                sendOffButton.Enabled = false;
                goalButton.BackColor = Color.Red;
                goalButton.Enabled = false;
                cautionButton.BackColor = Color.Red;
                cautionButton.Enabled = false;
                subOffButton.BackColor = Color.Red;
                subOffButton.Enabled = false;

                //Record the actual caution and resulting sending off
                cautionPlayerOff();
            }

            //Hide this screen to display the main Match screen
            this.Visible = false;
        }

        //This method is run if the chosen C1 code is 'UB'
        private void C1CodeUBBtn_Click(object sender, EventArgs e)
        {
            //Indicate the chosen C1 code
            code = "UB";

            //If this is the players first caution...
            if (!cautionButton.BackColor.Equals(Color.Yellow))
            {
                //Colour all relevant buttons on other screens
                //to indicate caution being recorded
                sendOffButton.BackColor = Color.Yellow;
                goalButton.BackColor = Color.Yellow;
                cautionButton.BackColor = Color.Yellow;
                subOffButton.BackColor = Color.Yellow;

                //Record the actual caution
                cautionPlayer();
            }
            //Otherwise this is the players second caution...
            else
            {
                //Inform the referee that the player should also be sent off
                MessageBox.Show("This player is being sent off", "Second Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                //Colour relevant buttons from other screens, and
                //disable them as a result of player being sent off
                sendOffButton.BackColor = Color.Red;
                sendOffButton.Enabled = false;
                goalButton.BackColor = Color.Red;
                goalButton.Enabled = false;
                cautionButton.BackColor = Color.Red;
                cautionButton.Enabled = false;
                subOffButton.BackColor = Color.Red;
                subOffButton.Enabled = false;

                //Record the actual caution and resulting sending off
                cautionPlayerOff();
            }

            //Hide this screen to display the main Match screen
            this.Visible = false;
        }

        //This method is called to obtain the GPS location
        //of the PDA device
        private String getGPS()
        {
            //Read location from the GPS antenna
            String gps_location = theMatch.gpsReader.readGPS();

            //Return the GPS sentence as a String
            return gps_location;
        }

        //This method is used to connect to the database
        private void connectToDatabase()
        {
            // Retrieve the connection string
            conString = @"Data Source=\Program Files\RefOffPDA\RefOffPDA_DB.sdf";

            // Open the connection using the connection string
            conn = new SqlCeConnection(conString);
            conn.Open();
        }

        //This method is used to disconnect from the database
        private void closeDatabaseConnection()
        {
            //Close the database connection
            conn.Close();
        }

        //This method is called when the 'Cancel' button is clicked
        private void CancelButton_Click(object sender, EventArgs e)
        {
            //Hide this screen to display the main Match screen
            this.Visible = false;
        }
    }
}